package com.ft.admix.config;

import com.ft.admix.common.AdType;

import java.util.HashMap;
import java.util.Map;

public class AdConfig {
    private String platformName;    // 广告平台名称
    private AdType adType;         // 广告类型
    private String appId;          // 应用ID
    private String adId;           // 广告位ID
    private int width;            // 广告宽度（Banner、信息流等可能需要）
    private int height;           // 广告高度
    private String userId;        // 用户ID（激励视频可能需要）
    private String rewardName;    // 奖励名称
    private int rewardAmount;     // 奖励数量
    private boolean isFullScreen; // 是否全屏（插屏广告可能需要）
    private int biddingType; //竞价类型(1.固价)
    private Map<String, Object> extraParams; // 扩展参数

    private AdConfig(Builder builder) {
        this.platformName = builder.platformName;
        this.adType = builder.adType;
        this.appId = builder.appId;
        this.adId = builder.adId;
        this.width = builder.width;
        this.height = builder.height;
        this.userId = builder.userId;
        this.rewardName = builder.rewardName;
        this.rewardAmount = builder.rewardAmount;
        this.isFullScreen = builder.isFullScreen;
        this.extraParams = builder.extraParams;
    }

    // Builder 模式
    public static class Builder {
        private String platformName;
        private AdType adType;
        private String appId;
        private String adId;
        private int width;
        private int height;
        private String userId;
        private String rewardName;
        private int rewardAmount;
        private boolean isFullScreen;
        private Map<String, Object> extraParams = new HashMap<>();

        public Builder setPlatformName(String platformName) {
            this.platformName = platformName;
            return this;
        }

        public Builder setAdType(AdType adType) {
            this.adType = adType;
            return this;
        }

        public Builder setAppId(String appId) {
            this.appId = appId;
            return this;
        }

        public Builder setAdId(String adId) {
            this.adId = adId;
            return this;
        }

        public Builder setSize(int width, int height) {
            this.width = width;
            this.height = height;
            return this;
        }

        public Builder setUserId(String userId) {
            this.userId = userId;
            return this;
        }

        public Builder setReward(String rewardName, int rewardAmount) {
            this.rewardName = rewardName;
            this.rewardAmount = rewardAmount;
            return this;
        }

        public Builder setFullScreen(boolean isFullScreen) {
            this.isFullScreen = isFullScreen;
            return this;
        }

        public Builder addExtra(String key, Object value) {
            this.extraParams.put(key, value);
            return this;
        }

        public AdConfig build() {
            // 参数验证
            validateConfig();
            return new AdConfig(this);
        }

        private void validateConfig() {
            if (platformName == null || platformName.trim().isEmpty()) {
                throw new IllegalArgumentException("Platform name cannot be empty");
            }
            if (adType == null) {
                throw new IllegalArgumentException("Ad type cannot be null");
            }
            if (adId == null || adId.trim().isEmpty()) {
                throw new IllegalArgumentException("Ad ID cannot be empty");
            }

            // 根据广告类型验证必要参数
            switch (adType) {
                case REWARD_VIDEO:
                    if (userId == null || userId.trim().isEmpty()) {
                        throw new IllegalArgumentException("User ID is required for reward video ad");
                    }
                    break;
                case BANNER:
                    if (width <= 0 || height <= 0) {
                        throw new IllegalArgumentException("Invalid banner size");
                    }
                    break;
                // 其他广告类型的验证...
            }
        }
    }

    // Getters
    public String getPlatformName() {
        return platformName;
    }

    public AdType getAdType() {
        return adType;
    }

    public String getAppId() {
        return appId;
    }

    public String getAdId() {
        return adId;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public String getUserId() {
        return userId;
    }

    public String getRewardName() {
        return rewardName;
    }

    public int getRewardAmount() {
        return rewardAmount;
    }

    public boolean isFullScreen() {
        return isFullScreen;
    }

    public Map<String, Object> getExtraParams() {
        return extraParams;
    }

    @SuppressWarnings("unchecked")
    public <T> T getExtra(String key, T defaultValue) {
        return (T) extraParams.getOrDefault(key, defaultValue);
    }

    @Override
    public String toString() {
        return "AdConfig{" +
                "platformName='" + platformName + '\'' +
                ", adType=" + adType +
                ", adId='" + adId + '\'' +
                ", width=" + width +
                ", height=" + height +
                ", userId='" + userId + '\'' +
                ", rewardName='" + rewardName + '\'' +
                ", rewardAmount=" + rewardAmount +
                ", isFullScreen=" + isFullScreen +
                ", extraParams=" + extraParams +
                '}';
    }
}